<?php
namespace Home\Controller;
use Common\Controller\HomeController;
class IndexController extends HomeController {
    public function index(){
        $cate=M('Category')->select();
        $this->commo=M('Commodity')->join(" fm_com_image ON fm_com_image.com_id=fm_commodity.id ")->select();
        $this->assign('productshow',U('/Home/Product/product_show'));
        $this->cates=getCategoryPid(0);
        $this->display();
        return getCategoryForm();
    }

    public function login(){
    	layout(false);
        $this->assign('loginact',U('/Home/Index/loginact'));
    	$this->display();
    }
    public function loginact(){
        layout(false);
        if (IS_GET) {

            $username=I('get.username');
            $password=md5(I('get.pwd'));
             //自动登录
            date_default_timezone_set("Asia/Shanghai");

            $homeuser=M('HomeUser');
            $pass=$homeuser->where("username='{$username}'")->find();
            $auto_login = I('get.auto_login');
            if (!empty($pass)) {
                if ($password==$pass['password']) {
                    if (!empty($auto_login)&&I('get.auto_login')==1) {
                        $key=base_convert(sha1(uniqid(mt_rand(),true)), 16, 36);
                        $start=time();
                        $start_date=date('Y-m-d H:i:s',$start);
                        $end=$start+7*24*3600;
                        $end_date=date('Y-m-d H:i:s',$end);
                        setcookie('webkey',$key,$end);
                        M('AutoLogin')->where("user_id={$pass['id']}")->delete();
                        $data['login_key']=$key;
                        $data['user_id']=$pass['id'];
                        $data['start_date']=$start_date;
                        $data['end_date']=$end_date;
                        M('AutoLogin')->add($data);
                    }
                    session('name',$pass['username']);
                    session('id',$pass['id']);
                    $this->success('登录成功！',U('/Home/Index/index'));
                }else{
                    $this->error('密码错误！');
                }
            }else{
                $this->error('没有这个用户，请先注册！');
            }
           
            
        }
    }

    public function verify()
    {
        ob_clean();
        // 实例化Verify对象
        $verify = new \Think\Verify();
        // 配置验证码参数
        $verify->fontSize = 18;     // 验证码字体大小
        $verify->length = 4;        // 验证码位数
        $verify->imageH = 44;       // 验证码高度
        $verify->useImgBg = true;   // 开启验证码背景
        $verify->useNoise = false;  // 关闭验证码干扰杂点
        $verify->entry();
    }

    public function reg(){
    	layout(false);
        // print_r($_SESSION);
        //  $key = substr(md5("ThinkPHP.CN"), 5, 8);
        // $str = substr(md5(session('cap')), 8, 10);
        // $code=md5($key . $str);
        // foreach ($_SESSION as $key => $value) {
        //     foreach ($value as $k => $v) {
        //         if ($k=="verify_code") {
        //             if ($code==$v) {
        //                 session('msg',"验证码正确");
        //             }else{
        //                 session('msg',"验证码错误");
        //             }
        //         }
        //     }
        // }

        // exit;
        
        $this->regact=U('/Home/Index/regact');
    	$this->display();
    }
    public function regact(){
        layout(false);
        if (IS_POST) {
            $usermodel=D('HomeUser');
            $captach=I('post.captach');
            //验证码
            function check_verify($code, $id = ''){
                $verify = new \Think\Verify();
                return $verify->check($code, $id);
            }
            $r=check_verify($captach);
            if (!$r) {
                $this->error('验证码错误');
            }
            $data['username']=I('post.username');
            $data['mail']=I('post.mail');
            $data['password']=md5(I('post.pwd1'));
            if ($usermodel->create($data)) {
               if ($usermodel->add()) {
                    $this->success('注册成功！',U('/Home/Index/login'));
                }else{
                   $this->error($usermodel->getError('注册失败111！'));
                }
            }else{
                $this->error($usermodel->getError('注册失败222！'));
            }
        }else{
            $this->error($usermodel->getError('注册失败333！'));
        }
    }

    public function loginout(){
        session(null);
        header("location:index");
    }

}